﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.Dto;
using LeadThing.AbpZeroTemplate.UserTrajectories;
using LeadThing.AbpZeroTemplate.UserTrajectorys.Authorization;
using LeadThing.AbpZeroTemplate.UserTrajectorys.Dtos;


namespace LeadThing.AbpZeroTemplate.UserTrajectorys
{
    /// <summary>
    /// 综治人员巡查轨迹服务实现
    /// </summary>
    //[AbpAuthorize(UserTrajectoryAppPermissions.UserTrajectory)]


    public class UserTrajectoryAppService : AbpZeroTemplateAppServiceBase, IUserTrajectoryAppService
    {
        private readonly IRepository<UserTrajectory, long> _userTrajectoryRepository;
        private readonly IUserTrajectoryListExcelExporter _userTrajectoryListExcelExporter;


        private readonly UserTrajectoryManage _userTrajectoryManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public UserTrajectoryAppService(IRepository<UserTrajectory, long> userTrajectoryRepository,
UserTrajectoryManage userTrajectoryManage
      , IUserTrajectoryListExcelExporter userTrajectoryListExcelExporter
  )
        {
            _userTrajectoryRepository = userTrajectoryRepository;
            _userTrajectoryManage = userTrajectoryManage;
            _userTrajectoryListExcelExporter = userTrajectoryListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<UserTrajectory> _userTrajectoryRepositoryAsNoTrack => _userTrajectoryRepository.GetAll().AsNoTracking();

        /// <summary>
        /// 根据UserId获取用户轨迹列表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<UserTrajectoryListDto> GetUserTrajectoryByUserId(GetUserTrajectoryInput input)
        {
            var query = _userTrajectoryRepository.GetAll();
            if (input.UserId > 0)
            {
                query = query.Where(p => p.UserId == input.UserId);
            }
            return query.ToList().MapTo<List<UserTrajectoryListDto>>();
        }


        #endregion


        #region 综治人员巡查轨迹管理

        /// <summary>
        /// 根据查询条件获取综治人员巡查轨迹分页列表
        /// </summary>
        public async Task<PagedResultDto<UserTrajectoryListDto>> GetPagedUserTrajectorysAsync(GetUserTrajectoryInput input)
        {

            var query = _userTrajectoryRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var userTrajectoryCount = await query.CountAsync();

            var userTrajectorys = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var userTrajectoryListDtos = userTrajectorys.MapTo<List<UserTrajectoryListDto>>();
            return new PagedResultDto<UserTrajectoryListDto>(
            userTrajectoryCount,
            userTrajectoryListDtos
            );
        }

        /// <summary>
        /// 通过Id获取综治人员巡查轨迹信息进行编辑或修改 
        /// </summary>
        public async Task<GetUserTrajectoryForEditOutput> GetUserTrajectoryForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetUserTrajectoryForEditOutput();

            UserTrajectoryEditDto userTrajectoryEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _userTrajectoryRepository.GetAsync(input.Id.Value);
                userTrajectoryEditDto = entity.MapTo<UserTrajectoryEditDto>();
            }
            else
            {
                userTrajectoryEditDto = new UserTrajectoryEditDto();
            }

            output.UserTrajectory = userTrajectoryEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取综治人员巡查轨迹ListDto信息
        /// </summary>
        public async Task<UserTrajectoryListDto> GetUserTrajectoryByIdAsync(EntityDto<long> input)
        {
            var entity = await _userTrajectoryRepository.GetAsync(input.Id);

            return entity.MapTo<UserTrajectoryListDto>();
        }







        /// <summary>
        /// 新增或更改综治人员巡查轨迹
        /// </summary>
        public async Task CreateOrUpdateUserTrajectoryAsync(CreateOrUpdateUserTrajectoryInput input)
        {
            if (input.UserTrajectoryEditDto.Id.HasValue)
            {
                await UpdateUserTrajectoryAsync(input.UserTrajectoryEditDto);
            }
            else
            {
                await CreateUserTrajectoryAsync(input.UserTrajectoryEditDto);
            }
        }

        /// <summary>
        /// 新增综治人员巡查轨迹
        /// </summary>
        [AbpAuthorize(UserTrajectoryAppPermissions.UserTrajectory_CreateUserTrajectory)]
        public virtual async Task<UserTrajectoryEditDto> CreateUserTrajectoryAsync(UserTrajectoryEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<UserTrajectory>();

            entity = await _userTrajectoryRepository.InsertAsync(entity);
            return entity.MapTo<UserTrajectoryEditDto>();
        }

        /// <summary>
        /// 编辑综治人员巡查轨迹
        /// </summary>
        [AbpAuthorize(UserTrajectoryAppPermissions.UserTrajectory_EditUserTrajectory)]
        public virtual async Task UpdateUserTrajectoryAsync(UserTrajectoryEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _userTrajectoryRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _userTrajectoryRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除综治人员巡查轨迹
        /// </summary>
        [AbpAuthorize(UserTrajectoryAppPermissions.UserTrajectory_DeleteUserTrajectory)]
        public async Task DeleteUserTrajectoryAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _userTrajectoryRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除综治人员巡查轨迹
        /// </summary>
        [AbpAuthorize(UserTrajectoryAppPermissions.UserTrajectory_DeleteUserTrajectory)]
        public async Task BatchDeleteUserTrajectoryAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _userTrajectoryRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 综治人员巡查轨迹的Excel导出功能


        public async Task<FileDto> GetUserTrajectoryToExcel()
        {
            var entities = await _userTrajectoryRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<UserTrajectoryListDto>>();

            var fileDto = _userTrajectoryListExcelExporter.ExportUserTrajectoryToFile(dtos);



            return fileDto;
        }


        #endregion

        #region APP

        /// <summary>
        /// 新增综治人员巡查轨迹
        /// </summary>
        public async Task<UserTrajectoryEditDto> CreateUserTrajectoryAPPAsync(UserTrajectoryEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增
            var entity = input.MapTo<UserTrajectory>();

            entity = await _userTrajectoryRepository.InsertAsync(entity);
            return entity.MapTo<UserTrajectoryEditDto>();
        }

        #endregion

    }
}
